package com.whq.thrift.api.thrift.as.service.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whq.common.StringUtil;
import com.whq.common.loadclass.DynamicLoadClassPathUtil;
import com.whq.thrift.api.common.constants.CommonErrorCodeConstants;
import com.whq.thrift.api.common.spring.ReloadableConfManager;
import com.whq.thrift.api.common.thriftserver.AbstractApiJsonService;
import com.whq.thrift.api.common.thriftserver.ResponseBase;
import com.whq.thrift.api.thrift.as.service.constants.AsMixedErrorCodeConstants;

@Service
public class AsBaseSpringService extends AbstractApiJsonService {
	private static final Logger LOGGER = LoggerFactory.getLogger(AsBaseSpringService.class);
	
	@Autowired
	private ReloadableConfManager reloadableConfManager = null;
	
	@Autowired
	private SqlSessionFactory sqlSessionFactory;
	
	@Autowired
	private SqlSession sqlSession;
	
//	@Autowired
//	private MemcachedClient memcachedClient01;
//	
//	@Autowired
//	private MemcachedClient memcachedClient02;
//	
//	@Autowired
//	private RedisHelper redisHelper01;
//	
//	@Autowired
//	private RedisHelper redisHelper02;
	
	private static boolean inited = false;
	
	private static final Map<String, ApiDetailInterface> MAP_METHOD_INSTANCE = new HashMap<String, ApiDetailInterface>(0);
	
	@Override
	protected ResponseBase doHandleApiJsonRequest(String method, Map<String, String> params, Map<String, String> ctx, String flowId) {
		if (StringUtil.isBlank(method)) {
			return new ResponseBase(CommonErrorCodeConstants.ERR_CODE_LACK_PARAM, "缺少参数：method");
		}
		
		loadMethodClass();
		
		ApiDetailInterface adi = MAP_METHOD_INSTANCE.get(method);
		if (adi == null) {
			String methodClassRootPath = reloadableConfManager.getPropertyValue("method.classpath.prefix", null, "com.whq.thrift.api.thrift.as.service.method");
			String classname = methodClassRootPath + "." + method.trim() + ".ApiDetailImpl";
			try {
				@SuppressWarnings("rawtypes")
				Class c = getClass().getClassLoader().loadClass(classname);
				adi = (ApiDetailInterface) c.newInstance(); // 从Class中实例出一个对象
				if (adi != null) {
					MAP_METHOD_INSTANCE.put(method, adi);
				}
			} catch (Exception e) {
				LOGGER.error("load class [" + classname + "] exception: ", e);
			}
		}
		
		if (adi != null) {
			try {
				return adi.execApi(this, method, params, ctx);
			} catch (Exception e) {
				LOGGER.error("excute [" + method + "] exception: ", e);
			}
		}
		
		// 其他业务
		return new ResponseBase(AsMixedErrorCodeConstants.ERR_CODE_EXEC_THRIFT_API_ERROR, "执行api接口[" + method + "]出错");
		
	}
	
	private void loadMethodClass() {
		if (inited) {
			return ;
		}
		inited = true;
		
		String methodClassRootPath = reloadableConfManager.getPropertyValue("method.class.rootpath", null, "");
		
		DynamicLoadClassPathUtil.loadClassPath(methodClassRootPath);
	}
	
	public ReloadableConfManager getReloadableConfManager() {
		return this.reloadableConfManager;
	}
	
	public SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}

	public SqlSession getSqlSession() {
		return sqlSession;
	}

//	public MemcachedClient getMemcachedClient01() {
//		return memcachedClient01;
//	}
//
//	public MemcachedClient getMemcachedClient02() {
//		return memcachedClient02;
//	}
//
//	public RedisHelper getRedisHelper01() {
//		return redisHelper01;
//	}
//
//	public RedisHelper getRedisHelper02() {
//		return redisHelper02;
//	}
}
